<?php

/**
 * Description of Critere
 *
 * @author Elodie
 * @version 1.0
 */
class Options {

    //ID
    protected $IdOptions;
    //String
    protected $KeyOptions;
    protected $ValueOptions;
    //enum
    protected $Type;
     //
    public $requeteOk;
    public $errormsg;

    //Constructeur
    public function __construct($id = 0) {

        if (empty($id)) {

            $this->IdOptions = 0;

            $this->KeyOptions = "";
            $this->ValueOptions = "";
            $this->Type = 2;

            $this->requeteOk = true;
            $this->errormsg = "";
        } else {

            $sql = SQL::getInstance()->getConnection();

            //On retourne l'entree demandee
            $query = 'SELECT * FROM Options WHERE IdOptions = :id';
            $c = $sql->prepare($query);
            $c->bindParam(':id', $id, PDO::PARAM_INT);

            //Execution de la requête retourne false si echec
            if ($c->execute()) {

                $result = $c->fetch(PDO::FETCH_OBJ);
                if (!empty($result)) {

                    $this->IdOptions = $result->IdOptions;

                    $this->ValueOptions = $result->ValueOptions;
                    $this->KeyOptions = $result->KeyOptions;
                    $this->Type = $result->TypeOptions;

                    $this->requeteOk = true;
                    $this->errormsg = "Option trouveE";
                } else {
                    $this->requeteOk = false;
                    $this->errormsg = "Cette option n'existe pas !";
                }
            } else {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
            }
        }
    }

    //Insertion d'une option
    public function insertOption() {
        $sql = SQL::getInstance()->getConnection();
        $query =
                'INSERT INTO Options
		(
			 IdOptions,
			 KeyOptions,
			 ValueOptions,
                         TypeOptions
		)
		VALUES
		(
			:idOptions,
			:keyOptions,
			:valueOptions,
                        :type
		)';
        $sql->beginTransaction();
        try {
            $c = $sql->prepare($query);

            $c->bindParam(':idOptions', $this->IdOptions, PDO::PARAM_INT);

            $c->bindParam(':keyOptions', $this->KeyOptions, PDO::PARAM_STR);
            $c->bindParam(':valueOptions', $this->ValueOptions, PDO::PARAM_STR);

            $c->bindParam(':type', $this->Type, PDO::PARAM_STR);

            if ($c->execute()) {
                $this->IdOptions = $sql->lastInsertId();
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Insertion reussie !";
            } else {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
                $this->IdOptions = 0;
            }
        } catch (PDOException $Exception) {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
            $this->IdOptions = 0;
        }
    }

    //Modification d'une option
    public function updateOption() {
        $sql = SQL::getInstance()->getConnection();

        $query =
                'UPDATE Options SET
                    KeyOptions = :keyOptions,
                    ValueOptions = :valueOptions,
                    TypeOptions = :type
		WHERE
                    IdOptions = :id
		';

        $sql->beginTransaction();
        try {
            $c = $sql->prepare($query);

            $c->bindParam(':keyOptions', $this->KeyOptions, PDO::PARAM_STR);
            $c->bindParam(':valueOptions', $this->ValueOptions, PDO::PARAM_STR);
            $c->bindParam(':type', $this->Type, PDO::PARAM_STR);
            $c->bindParam(':id', $this->IdOptions, PDO::PARAM_INT);
            if ($c->execute()) {
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Modification reussie!";
            } else {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
            }
        } catch (PDOException $Exception) {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
        }
    }

    //Suppression d'une option
    public function deleteOption() {
        $sql = SQL::getInstance()->getConnection();

        $query = 'DELETE FROM Options WHERE IdOptions = :id';
        $c = $sql->prepare($query);
        $c->bindParam(':id', $this->IdOptions, PDO::PARAM_INT);
        $c->execute();
    }

//********************** Getter ************************

    public function getIdOptions() {
        return $this->IdOptions;
    }

    public function getKeyOptions() {
        return $this->KeyOptions;
    }

    public function getValueOptions() {
        return $this->ValueOptions;
    }

    public function getTypeOptions()
    {
        return $this->Type;
    }

    /**
     * Renvoie la liste des documents administratif
     * @static
     * @return mixed Array de document administratif
     */
    static public function getListDocumentAdministratif(){
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT * FROM Options WHERE TypeOptions='0'";
        $c = $sql->query($query);
        $result = $c->fetchAll(PDO::FETCH_OBJ);

        if (!empty($result))
        {
            return $result;
        }
        return array();
	}

    static public function getList() {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT * FROM Options ";
        $c = $sql->query($query);
        $result = $c->fetchAll(PDO::FETCH_OBJ);
        if(!empty($result))
        {
            return $result;
        }
        return array();
    }

//********************** Setter ***********************

    public function setKeyOptions($value) {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value != "" && strlen($value) <= 255) {
            $this->KeyOptions = htmlentities($value);
        } else {
            if (!isset($value)) {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La cle ne peut etre null !";
            }
            if (isset($value) && $value == "") {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La cle ne peut etre vide !";
            }
            if (isset($value) && strlen($value) > 255) {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La cle ne peut etre aussi longue !";
            } else {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cette cle n'est pas valide !";
            }

            $this->KeyOptions = 0;
        }
        return $errTab;
    }

    public function setValueOptions($value) {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value != "" && strlen($value) <= 255) {
            $this->ValueOptions = htmlentities($value);
        } else {
            if (!isset($value)) {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La valeur ne peut etre null !";
            }
            if (isset($value) && $value == "") {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La valeur ne peut etre vide !";
            }
            if (isset($value) && strlen($value) > 255) {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La valeur ne peut etre aussi longue !";
            } else {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cette valeur n'est pas valide !";
            }

            $this->ValueOptions = 0;
        }
        return $errTab;
    }

    public function setTypeOptions($value) {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value))
        {
            $this->Type = $value;
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le type ne peut etre null !";
            }
            if (isset($value) && $value == "")
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le type ne peut etre vide !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Ce type n'est pas valide !";
            }

            $this->Type = 2;
        }
        return $errTab;
    }


 //********************* Autres ***********************
    static public function globalCount()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT COUNT(IdUFR) as count FROM UFR";
        $c = $sql->query($query);
        $result = $c->fetch(PDO::FETCH_OBJ);
        return $result->count;
    }

    /**
     * Renvoie true si le document adimistratif exite, sinon renvoie false
     * @static
     * @param String $nomfichier
     * @param String $nomDoc
     * @return boolean
     */
    static public function isExistDocumentAdministratif($nomfichier, $nomDoc) {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT * FROM Options WHERE TypeOptions='0' AND ValueOptions = :nomfichier OR KeyOptions = :nomDoc";
        $c = $sql->prepare($query);
        $c->bindParam(':nomfichier', $nomfichier, PDO::PARAM_STR);
        $c->bindParam(':nomDoc', $nomDoc, PDO::PARAM_STR);
        $c->execute();
        $result = $c->fetch(PDO::FETCH_OBJ);
        //Si rien n'est renvoyé le fichier n'existe pas on renvoie false
        if (empty($result)) {
            return false;
        }
        // Sinon le fichier existe déjà on renvoie true
        else {
            return true;
        }

    }
}

?>
